package com.mrli.cat.weather.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mrli.cat.weather.R;
import com.mrli.cat.weather.database.WeatherContract;
import com.mrli.cat.weather.tools.Utility;

public class ForecastAdapter extends CursorAdapter {

    private Context mContext;
    private static final int VIEW_TYPE_COUNT = 2;
    private static final int VIEW_TYPE_TODAY = 0;
    private static final int VIEW_TYPE_FUTURE_DAY = 1;

    public ForecastAdapter(Context context, Cursor c, int i) {
        super(context, c, i);
        this.mContext = context;
    }

    //绑定到视图的视图对象
    public static class ViewHolder {
        public final ImageView iconView;
        public final TextView dateView;
        public final TextView descriptionView;
        public final TextView highTempView;
        public final TextView lowTempview;

        public ViewHolder(View view) {
            iconView = (ImageView) view.findViewById(R.id.list_item_icon);
            dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
            descriptionView = (TextView) view.findViewById(R.id.list_item_desc);
            highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
            lowTempview = (TextView) view.findViewById(R.id.list_item_low_textview);
        }

    }


    //创建一个新的列表项
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int viewType = getItemViewType(cursor.getPosition());
        int layoutId = -1;
        switch (viewType) {
            case VIEW_TYPE_TODAY: {
                layoutId = R.layout.list_item_forecast_today;
                break;
            }
            case VIEW_TYPE_FUTURE_DAY: {
                layoutId = R.layout.list_item_forecast;
                break;
            }
        }
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
        return view;
    }

    //绑定数据到视图
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        //气象图标
        int viewType = getItemViewType(cursor.getPosition());
        int weatherId = cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WEATHER));
        switch (viewType) {
            case VIEW_TYPE_TODAY: {
                viewHolder.iconView.setImageResource(Utility.convertIDtoWeatherArtIcon(weatherId));
                break;
            }
            case VIEW_TYPE_FUTURE_DAY: {
                viewHolder.iconView.setImageResource(Utility.convertIDtoWeatherICIcon(weatherId));
                break;
            }
        }
        //气象描述
        viewHolder.descriptionView.setText(Utility.convertIDtoDes(weatherId));
        //预报时间
        long timestamp = cursor.getLong(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TIME));
        viewHolder.dateView.setText(Utility.convertTimeStampToDate(timestamp));
        //最高气温和最低气温
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        String value = sharedPreferences.getString(WeatherPreferenceActivity.METRIC_KEY, "0");
        Boolean convert;
        if (value.equals("0")) {
            convert = false;
        } else {
            convert = true;
        }
        int highTemp = cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TEMP_MAX));
        viewHolder.highTempView.setText(Utility.convertCelsiusToFahrenheit(highTemp, convert));
        int lowTemp = cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TEMP_MIN));
        viewHolder.lowTempview.setText(Utility.convertCelsiusToFahrenheit(lowTemp, convert));
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }
}
